package com.baoyang.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baoyang.entity.BaoyangUser;
import com.baoyang.entity.Container;
import com.baoyang.entity.ProductName;
import com.baoyang.entity.vo.BatchSetArrivalStationReqVO;
import com.baoyang.entity.vo.ExportContainerByStatusReqVO;
import com.baoyang.entity.vo.ExportContainerReqVO;
import com.baoyang.entity.vo.ProductNameCountRespVO;
import com.baoyang.service.IContainerService;
import com.baoyang.service.IProductNameService;
import com.baoyang.tool.api.R;
import com.baoyang.tool.utils.SecurityUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 * 货柜信息表 前端控制器
 * </p>
 *
 * @author 金佳军
 * @since 2025-07-05
 */
@RestController
@RequestMapping("/container")
public class ContainerController {

    @Resource
    private IContainerService containerService;

    @Resource
    private IProductNameService productNameService;

    /**
     * 获取货柜列表
     * 
     * @param pageNum
     * @param pageSize
     * @param boxNo
     * @param driverId
     * @param transportationStatus
     * @param type
     * @return
     */
    @GetMapping("/containerList")
    public R list(@RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize,
            @RequestParam(required = false) String boxNo,
            @RequestParam(required = false) Long productNameId,
            @RequestParam(required = false) String shippingStation,
            @RequestParam(required = false) String arrivalStation,
            @RequestParam(required = false) String shipmentDate,
            @RequestParam(required = false) Long driverId,
            @RequestParam(required = false) Integer transportationStatus,
            @RequestParam(required = false) Integer type,
            @RequestParam(required = false) String dataScope) {
        Page<Container> page = new Page<>(pageNum, pageSize);
        IPage<Container> userPage = containerService.pageContainerList(page, boxNo, productNameId, shippingStation, arrivalStation, shipmentDate, driverId, transportationStatus,
                type, dataScope);
        return R.data(userPage);
    }

    /**
     * 添加货柜
     * 
     * @param container
     * @return
     */
    @PostMapping("/add")
    public R add(@RequestBody Container container) {
        boolean save = containerService.addContainer(container);
        if (save) {
            return R.success("添加成功");
        }
        return R.fail("添加失败");
    }

    /**
     * 设置到站
     */
    @PostMapping("/setArrivalStation")
    public R setArrivalStation(@RequestBody Container container) {
        boolean update = containerService.setArrivalStation(container);
        if (update) {
            return R.success("设置成功");
        }
        return R.fail("设置失败");
    }

    /**
     * 编辑货柜
     */
    @PostMapping("/edit")
    public R edit(@RequestBody Container container) {
        boolean update = containerService.editContainer(container);
        if (update) {
            return R.success("编辑成功");
        }
        return R.fail("编辑失败");
    }

    /**
     * 批量到站
     */
    @PostMapping("/batchSetArrivalStation")
    public R batchSetArrivalStation(@RequestBody List<BatchSetArrivalStationReqVO> reqVOS) {
        boolean update = containerService.batchSetArrivalStation(reqVOS);
        if (update) {
            return R.success("设置成功");
        }
        return R.fail("设置失败");
    }

    /**
     * 查询用户提交的箱柜
     */
    @GetMapping("/queryUserContainer")
    public R queryUserContainer() {
        String userId = SecurityUtils.getCurrentUserId();
        if (userId != null) {
            return R.data(containerService.queryUserContainer(Long.valueOf(userId)));
        }
        return R.fail("token错误");
    }

    /**
     * 提交
     */
    @PostMapping("/commit")
    public R commit(@RequestBody Container container) {
        boolean update = containerService.commit(container);
        if (update) {
            return R.success("提交成功");
        }
        return R.fail("提交失败");
    }

    /**
     * 撤销
     */
    @PostMapping("/cancel")
    public R cancel(@RequestBody Container container) {
        boolean update = containerService.cancel(container);
        if (update) {
            return R.success("撤销成功");
        }
        return R.fail("撤销失败");
    }

    /**
     * 卸货
     */
    @PostMapping("/unload")
    public R unload(@RequestBody Container container) {
        boolean update = containerService.unload(container);
        if (update) {
            return R.success("卸货成功");
        }
        return R.fail("卸货失败");
    }

    /**
     * 根据箱号获取箱柜
     */
    @GetMapping("/getByBoxNo/{boxNo}")
    public R getByBoxNo(@PathVariable("boxNo") String boxNo) {
        Container container = containerService.getOne(new QueryWrapper<Container>().eq("box_no", boxNo).ne("transportation_status", 4));
        if (container.getTransportationStatus().equals(1)){
            return R.fail("该箱柜未到站");
        } else if (container.getTransportationStatus().equals(3)) {
            return R.fail("该箱柜已被提交");
        }
        ProductName productName = productNameService.getById(container.getProductNameId());
        container.setProjectName(productName.getName());
        return R.data(container);
    }

    /**
     * 删除货柜
     */
    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable("id") String id) {
        boolean update = containerService.removeById(id);
        if (update) {
            return R.success("删除成功");
        }
        return R.fail("删除失败");
    }

    /**
     * 小程序首页货柜分页
     */
    @GetMapping("/getAllArrivedPage")
    public R getAllArrived(@RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize,
            @RequestParam(required = false) Long projectNameId,
            @RequestParam(required = false) Integer transportationStatus,
            @RequestParam(required = false) String boxNo,
            @RequestParam(required = false) String dataScope) {
        Page<Container> page = new Page<>(pageNum, pageSize);
        IPage<Container> containerPage = containerService.getAllArrivedPage(page, projectNameId, transportationStatus,
                boxNo, dataScope);
        return R.data(containerPage);
    }

    /**
     * 首页数据看板
     */
    @GetMapping("/dashboard")
    public R dashboard() {
        return R.data(containerService.dashboard());
    }

    /**
     * 点击数据看板-未到站、已到站、运输中列表
     */
    @GetMapping("/dashboard/list")
    public R dashboardList(@RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize,
            @RequestParam(required = false) Long productNameId,
            @RequestParam(required = false) Integer transportationStatus) {
        Page<Container> page = new Page<>(pageNum, pageSize);
        IPage<Container> containerPage = containerService.dashboardList(page, productNameId, transportationStatus);
        return R.data(containerPage);
    }

    /**
     * 点击数据看板-今天卸货列表
     */
    @GetMapping("/dashboard/todayUnloadList")
    public R dashboardTodayUnloadList(@RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize,
            @RequestParam(required = false) String driverName) {
        Page<Container> page = new Page<>(pageNum, pageSize);
        IPage<Container> containerPage = containerService.todayUnloadList(page, driverName);
        return R.data(containerPage);
    }

    /**
     * 我的运输-分页
     */
    @GetMapping("/myTransportationPageList")
    public R myTransportationPageList(@RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize,
            @RequestParam(required = false) String boxNo,
            @RequestParam(required = false) String startTime,
            @RequestParam(required = false) String endTime) {
        Page<Container> page = new Page<>(pageNum, pageSize);
        IPage<Container> containerPage = containerService.myTransportationPageList(page, boxNo, startTime, endTime);
        return R.data(containerPage);
    }

    /**
     * 我的运输-上方统计
     */
    @GetMapping("/myTransportationCount")
    public R myTransportationCount() {
        return R.data(containerService.myTransportationCount());
    }

    /**
     * 钓箱追踪-分页
     */
    @GetMapping("/fishingBoxTrackingPageList")
    public R fishingBoxTrackingPageList(@RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize,
            @RequestParam(required = false) String boxNo,
            @RequestParam(required = false) Long productNameId) {
        Page<Container> page = new Page<>(pageNum, pageSize);
        IPage<Container> containerPage = containerService.fishingBoxTrackingPageList(page, boxNo, productNameId);
        return R.data(containerPage);
    }

    /**
     * 货物品名-未到站、已到站、运输中 箱规统计
     */
    @GetMapping("/productNameCountList")
    public R productNameNotArrivedCount(@RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) Integer transportationStatus) {

        IPage<ProductNameCountRespVO> result = containerService.productNameNotArrivedCount(pageNum, pageSize,
                transportationStatus);
        return R.data(result);
    }

    /**
     * 根据司机id获取已卸货的货柜列表
     */
    @GetMapping("/getUnloadedContainersByDriverIdPageList")
    public R getUnloadedContainersByDriverIdPageList(@RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String startTime,
            @RequestParam(required = false) String endTime,
            @RequestParam(required = false) String id) {
        Page<Container> page = new Page<>(pageNum, pageSize);
        IPage<Container> userPage = containerService.getUnloadedContainersByDriverIdPageList(page, startTime, endTime,
                id);
        return R.data(userPage);
    }

    /**
     * PC端卸货统计折线图-15天
     */
    @GetMapping("/pcUnloadCount")
    public R pcUnloadCount() {
        return R.data(containerService.pcUnloadCount());
    }

    /**
     * PC端获取已卸货的货柜信息
     */
    @GetMapping("/pcUnloadPageList")
    public R pcUnloadPageList(@RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<Container> page = new Page<>(pageNum, pageSize);
        IPage<Container> containerIPage = containerService.pcUnloadPageList(page);
        return R.data(containerIPage);
    }

    /**
     * excel导入货柜
     */
    @PostMapping("/importByExcel")
    public R importByExcel(@RequestParam("file") MultipartFile file) {
        return R.status(containerService.importByExcel(file));
    }

    /**
     * 导出已卸货货柜Excel
     */
    @PostMapping("/exportExcel")
    public void exportExcel(@RequestBody ExportContainerReqVO reqVO, HttpServletResponse response) {
        containerService.exportContainerExcel(reqVO, response);
    }

    /**
     * 导出其他状态货柜Excel
     */
    @PostMapping("/exportExcelByStatus")
    public void exportExcelByStatus(@RequestBody ExportContainerByStatusReqVO reqVO, HttpServletResponse response) {
        containerService.exportExcelByStatus(reqVO, response);
    }

    /**
     * 批量删除货柜
     */
    @PostMapping("/batchDelete")
    public R batchDelete(@RequestBody List<String> ids) {
        return R.status(containerService.batchDelete(ids));
    }

}
